/** 
 * File-Name:ArticleAction.java
 *
 * Created on 2011-6-2 下午10:10:52
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package xiaoyou.web;

import java.io.File;
import java.io.InputStream;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import xiaoyou.entity.Article;
import xiaoyou.entity.User;
import xiaoyou.neoutil.CommonAction;
import xiaoyou.neoutil.CommonService;
import xiaoyou.neoutil.FileUtils;
import xiaoyou.neoutil.Page;
import xiaoyou.service.ArticleRepository;

/**
 * Description:
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2011-6-2 下午10:10:52
 */
public class ArticleAction extends CommonAction {
	private static final long serialVersionUID = 8552938317575505315L;
	private static Logger log = Logger.getLogger(ArticleAction.class);
	private Article article;
	private File articleImage;
	private String articleImageContentType;
	private String articleImageFileName;
	private String imgFile;
	private String imgFileContentType;
	private String imgFileFileName;
	private File articleFile;
	private String articleFileContentType;
	private String articleFileFileName;
	private String imageSavePath;
	private String fileSavePath;
	private Page<Article> page = new Page<Article>(15);
	private int type;
	private int part;
	private String searchCondition;

	public String toAddArticle() {
		try {
			User user = (User) getSessionObject("loginUser");
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else {
				type = Integer.parseInt(getRequestParameter("type"));
				part = Integer.parseInt(getRequestParameter("part"));
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}

	}

	public String addArticle() {
		try {
			User user = (User) getSessionObject("loginUser");
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else {
				article.setAuthor(user);

				if (articleImage != null) {
					String imageName = article.getUuid()
							+ FileUtils.getExtention(getArticleImageFileName());
					FileUtils.write(getImageSavePath() + "\\", imageName,
							getArticleImage());

					article.setImagePath(imageName);
				}
				if (articleFile != null) {
					String fileName = article.getUuid()
							+ FileUtils.getExtention(getArticleFileFileName());
					FileUtils.write(getFileSavePath() + "\\", fileName,
							getArticleFile());

					article.setFilePath(fileName);
				}

				ArticleRepository articleRepository = new ArticleRepository();
				if (CommonService.SUCCESS == articleRepository.save(article)) {
					return SUCCESS;
				} else {
					return INPUT;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String downloadArticleEnclosure() {
		return SUCCESS;
	}

	public InputStream getEnclosureFile() {
		try {
			String uuid = getRequestParameter("uuid");
			article = getArticleRepository().get(uuid);

			System.err
					.println(getFileSavePath() + "\\" + article.getFilePath());

			return new java.io.FileInputStream(getFileSavePath() + "\\"
					+ article.getFilePath());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String toEditArticle() {
		try {
			User user = (User) getSessionObject("loginUser");
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else {
				String uuid = getRequestParameter("uuid");
				if (uuid == null || "".equals(uuid)) {
					return INPUT;
				} else {
					ArticleRepository articleRepository = new ArticleRepository();
					article = articleRepository.get(uuid);
					part = Integer.parseInt(getRequestParameter("part"));
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String editArticle() {
		try {
			User user = (User) getSessionObject("loginUser");
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else {

				if (articleImage != null) {
					String imageName = article.getUuid()
							+ FileUtils.getExtention(getArticleImageFileName());
					FileUtils.write(getImageSavePath() + "\\", imageName,
							getArticleImage());

					article.setImagePath(imageName);
				}
				if (articleFile != null) {
					String fileName = article.getUuid()
							+ FileUtils.getExtention(getArticleFileFileName());
					FileUtils.write(getFileSavePath() + "\\", fileName,
							getArticleFile());

					article.setFilePath(fileName);
				}

				ArticleRepository articleRepository = new ArticleRepository();
				article.setAuthor(user);
				if (CommonService.SUCCESS == articleRepository.update(article)) {
					return SUCCESS;
				} else {
					return INPUT;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String deleteArticle() {
		try {
			User user = (User) getSessionObject("loginUser");
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else {
				long id = Long.parseLong(getRequestParameter("id"));
				ArticleRepository articleRepository = new ArticleRepository();
				if (CommonService.SUCCESS == articleRepository.delete(id)) {
					return SUCCESS;
				} else {
					return INPUT;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String getArticleByUuid() {
		try {
			String uuid = getRequestParameter("uuid");
			String part = getRequestParameter("part");
			if (uuid == null || "".equals(uuid) || part == null
					|| "".equals(part)) {
				return INPUT;
			} else {
				this.part = Integer.parseInt(part);
				ArticleRepository articleRepository = new ArticleRepository();
				article = articleRepository.get(uuid);
				type = article.getType();
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String getArticlesByType() {
		try {			
			searchCondition = getRequestParameter("searchCondition");
			if (searchCondition != null && !"".equals(searchCondition)) {
				this.part = 9;
				searchCondition = new String(searchCondition.getBytes("iso-8859-1"), "UTF-8");
				System.err.println("Query:"
						+ new String(searchCondition.getBytes("iso-8859-1"), "UTF-8"));
				ArticleRepository articleRepository = new ArticleRepository();
				page = articleRepository.search(page, searchCondition);
				return SUCCESS;
			}

			String part = getRequestParameter("part");
			String type = getRequestParameter("type");
			if (type == null || "".equals(type) || part == null
					|| "".equals(part)) {
				log.error("getArticlesByType传入参数错误！！");
				return INPUT;
			} else {
				this.part = Integer.parseInt(part);

				this.type = Integer.parseInt(type);
				ArticleRepository articleRepository = new ArticleRepository();
				page = articleRepository.getArticlesByType(page, this.type);
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String getLatestArticleByType() {
		try {
			String part = getRequestParameter("part");
			String type = getRequestParameter("type");
			if (type == null || "".equals(type) || part == null
					|| "".equals(part)) {
				return INPUT;
			} else {
				this.part = Integer.parseInt(part);

				this.type = Integer.parseInt(type);
				ArticleRepository articleRepository = new ArticleRepository();
				article = articleRepository.getTheLatestByType(this.type);
				return SUCCESS;
			}

		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String searchArticles() {
		try {
			searchCondition = getRequestParameter("query");
			if (searchCondition == null || "".equals(searchCondition)) {
				return INPUT;
			} else {
				this.part = 9;

				System.err.println("Query:"
						+ new String(searchCondition.getBytes("iso-8859-1"), "UTF-8"));
				ArticleRepository articleRepository = new ArticleRepository();
				page = articleRepository.search(page, new String(searchCondition
						.getBytes("iso-8859-1"), "UTF-8"));
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	/**
	 * Description:
	 * 
	 * @param args
	 *            void
	 */
	public static void main(String[] args) {
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public Page<Article> getPage() {
		return page;
	}

	public void setPage(Page<Article> page) {
		this.page = page;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getPart() {
		return part;
	}

	public void setPart(int part) {
		this.part = part;
	}

	public File getArticleImage() {
		return articleImage;
	}

	public void setArticleImage(File articleImage) {
		this.articleImage = articleImage;
	}

	public String getArticleImageContentType() {
		return articleImageContentType;
	}

	public void setArticleImageContentType(String articleImageContentType) {
		this.articleImageContentType = articleImageContentType;
	}

	public String getArticleImageFileName() {
		return articleImageFileName;
	}

	public void setArticleImageFileName(String articleImageFileName) {
		this.articleImageFileName = articleImageFileName;
	}

	public File getArticleFile() {
		return articleFile;
	}

	public void setArticleFile(File articleFile) {
		this.articleFile = articleFile;
	}

	public String getArticleFileContentType() {
		return articleFileContentType;
	}

	public void setArticleFileContentType(String articleFileContentType) {
		this.articleFileContentType = articleFileContentType;
	}

	public String getArticleFileFileName() {
		return articleFileFileName;
	}

	public void setArticleFileFileName(String articleFileFileName) {
		this.articleFileFileName = articleFileFileName;
	}

	@SuppressWarnings("deprecation")
	public String getImageSavePath() {
		return ServletActionContext.getRequest().getRealPath(imageSavePath);
	}

	public void setImageSavePath(String imageSavePath) {
		this.imageSavePath = imageSavePath;
	}

	public String getFileSavePath() {
		return ServletActionContext.getRequest().getRealPath(fileSavePath);
	}

	public void setFileSavePath(String fileSavePath) {
		this.fileSavePath = fileSavePath;
	}

	public String getImgFile() {
		return imgFile;
	}

	public void setImgFile(String imgFile) {
		this.imgFile = imgFile;
	}

	public String getImgFileContentType() {
		return imgFileContentType;
	}

	public void setImgFileContentType(String imgFileContentType) {
		this.imgFileContentType = imgFileContentType;
	}

	public String getImgFileFileName() {
		return imgFileFileName;
	}

	public void setImgFileFileName(String imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}

	public void setSearchCondition(String searchCondition) {
		this.searchCondition = searchCondition;
	}

	public String getSearchCondition() {
		return searchCondition;
	}

}
